﻿angular.module('app.directives', ['ngAnimate'])
	.directive('bgSlider', ['$rootScope', '$timeout', function ($rootScope, $timeout) {
		return {
			restrict: 'AE',
			replace: true,
			link: function (scope, elem, attrs) {
				scope.bgvisible = true;

				$rootScope.siteSettingsLoaded.promise.then(function () {
					if ($rootScope.staticResources && $rootScope.staticResources.bgImages) {
						scope.images = $rootScope.staticResources.bgImages;
					}

					if (scope.images && scope.images.length && scope.images.length > 0) {
						scope.currentIndex = Math.floor(Math.random() * scope.images.length);
					}
				});

				var randomBGImage = function () {
					var prev = scope.currentIndex;

					while (prev === scope.currentIndex) {
						scope.currentIndex = Math.floor(Math.random() * scope.images.length);
					}
				};

				var timer;

				var sliderFunc = function () {
					timer = $timeout(function () {
						scope.bgvisible = !scope.bgvisible;
						$timeout(function () {
							randomBGImage();
						}, 1000);
						$timeout(function () {
							scope.bgvisible = !scope.bgvisible;
						}, 1000);
						timer = $timeout(sliderFunc, 60000);
					}, 60000);
				};

				sliderFunc();

				scope.$on('$destroy', function () {
					$timeout.cancel(timer);
				});
			},
			templateUrl: '/Content/Responsive/src/shared/background-page-slider.html'
		};
	}])

	.directive("userPanel", ['$rootScope', function ($rootScope) {
		return {
			replace: true,
			templateUrl: '/Content/Responsive/src/shared/user-panel.html',
			link: function(scope, element, attrs) {
				$rootScope.siteSettingsLoaded.promise.then(function () {
					scope.user = $rootScope.user;
				});
			}
		};
	}])

	.directive("nscroll", ['$rootScope', '$timeout', function ($rootScope, $timeout) {
		return {
			restrict: 'A',
			link: function (scope, element, attrs) {
				//var update = function () {
				//	$(element).getNiceScroll().resize();
				//};

				$(element).niceScroll({
					cursorcolor: '#a71e2b',
					cursorborder: 0,
					zindex: 99999,
				});

				//$(element).scroll(update());
			}
		};
	}]);